var host = "http://sg.m/";
$(document).ready(function () {
    $("#PageValue").val("1");

});

function doRating(domId) {
    for (i = 1; i <= 5; i++) {
        if (i <= domId) {
            $("#" + i).addClass("ratings_star_on");
            $("#" + i).removeClass("ratings_star_off")
        } else {
            $("#" + i).addClass("ratings_star_off");
            $("#" + i).removeClass("ratings_star_on")
        }
    }
    $("#hdRating").attr("value", domId);
}

$(document).ready(function () {
    $("#btnSubmit").click(function (e) {
        
        var rating = $("#hdRating").val();
        var text = $("#txtProductReviewText").val();
        if ($.trim(text).length == 0) {
            Message("请输入评价内容,不能超过500个字符");
            $("#txtProductReviewText").focus();
            return false;
        }
        var valid = $('#yzm').val();
        if ($.trim(valid).length == 0) {
            Message("请输入验证码");
            $('#imgChange').focus();
            return false;
        }
        var productid = $("#productid").val();
        e.preventDefault();
        var data = { txt: text, rating: rating, valid: valid };

        $.post("ucenter/AddProductReview.aspx?productid=" + productid + "&act=addProductReview", data, function (result) {
            if (result.Msg === 'Ok') {
                Message("成功评论");
                $("#imgChange").trigger("click");
            } else {
                Message(result.Msg);
            }
        });
        
        //$.ajax({
        //    cache: false,
        //    type: "post",
        //    dataType: "json",
        //    url: window.location.href,
        //    data: "text=" + text + "&rating=" + rating + "&valid=" + valid + "&Event=AddReviews",
        //    error: function (data) {
        //        Message(data.responseText);
        //    },
        //    success: function (data) {
        //        if (data.Msg == "Ok") {
        //            Message("感谢参与，您的评论与建议将是我们成长的动力，我们在审核后即可查看。");
        //            $("#txtProductReviewText").val("");
        //            $('#yzm').val("");
        //        }
        //        else {
        //            Message(data.Msg);
        //        }
        //        $("#imgChange").click();
        //    }
        //});
    });
});


function loadComments(pid) {
    var page = $("#PageValue").val();
    var url = baseuri + "service/data.asmx/GetProductReview?productid=" + pid;
    $.ajax({
        cache: false,
        asyns: false,
        dataType: "json",
        url: url,
        data: "pageIndex=" + page,
        error: function (data) {
            Message("信息加载失败请与管理员联系！");
        },
        success: function (result) {
            var table = $(".comment-lists");
            table.empty();
            var total;
            var data = result.Obj;
            for (var i = 0; i < data.length; i++) {
                var dom = "<dl>"
					        + "<dt><span>" + data[i].CustomerName + "</span><span>" + data[i].CreatedOn + "</span><span class=\"stars star" + data[i].Rating + "\"><i></i><i></i><i></i><i></i><i></i></span></dt>"
					        + "<dd>"
					        + data[i].content
					        + "</dd>";
                if (data[i].title) {
                    dom += "<dd class=\"reply\">"
					+ "	<span>客服回复：</span>" + data[i].title
					+ "</dd>";
                }
                dom += "</dl>";

                table.append(dom);
                total = data[i].total;
                $("#ComentCount").html(total);
            }
            getPagerHtml(parseInt(page), total, 10);
        }
    });
}
function showReviewPage(page) {
    $("#PageValue").val(page);
    loadComments();
    offset = $(".detail_nav").offset();
    window.scrollTo(offset.left, offset.top);

}

//生成分页导航
function getPagerHtml(intPageIndex, total) {
    //pageS总页码
    var pageS = total
    if (pageS % 10 == 0) pageS = pageS / 10;
    else pageS = parseInt(total / 10) + 1;
    var $pager = $("#pager");

    //清除分页div中的内容
    $("#pager").html("");

    //设置分页的格式  
    if (pageS > 1) {

        //添加上一页
        if (intPageIndex == 1)
            $pager.append("<span class='disabled'><b><</b>上一页</span>");
        else {
            var pre = $("<a href='javascript:void(0)' onclick='showReviewPage(" + (intPageIndex - 1) + ")' pre='" + (intPageIndex - 1) + "'><b><</b>上一页</a>");
            $pager.append(pre);
        }

        var _s = (intPageIndex - 3 < 1) ? 1 : intPageIndex - 3;
        var _e = (intPageIndex + 2) >= pageS ? pageS : intPageIndex + 2;

        if (_s > 1) {
            var pageHtml = '<a href=javascript:void(0) onclick=showReviewPage(1)>1</a>';
            $pager.append(pageHtml);
            if (intPageIndex > 5) {
                var pageHtml = '<span class=\"text\">…</span>';
                $pager.append(pageHtml);
            }
        }
        if (_s < pageS) {
            for (var i = _s; i <= _e; i++) {
                if (i == intPageIndex) {
                    var pageHtml = $("<span class='current'>" + i + "</span>");
                } else {
                    var pageHtml = '<a ' + ((intPageIndex == i) ? "class=current" : "") + ' href=javascript:void(0) onclick=showReviewPage(' + i + ')>' + i + '</a>';
                }
                $pager.append(pageHtml);

            }
        }
        if (pageS > _e) {
            if ((pageS - intPageIndex) > 3) {
                var pageHtml = '<span class=\"text\">…</span>';
                $pager.append(pageHtml);
            }
            var pageHtml = '<a ' + ((intPageIndex == pageS) ? "class=current" : "") + ' href=javascript:void(0) onclick=showReviewPage(' + pageS + ')>' + (pageS) + '</a>';
            $pager.append(pageHtml);
        }


        //下一页
        if (intPageIndex == pageS) {
            $pager.append("<span class='disabled'>下一页<b>></b></span>");

        }
        else {
            var next = $("<a href='javascript:void(0)' onclick='showReviewPage(" + (intPageIndex + 1) + ")' >下一页<b>></b></a>");
            $pager.append(next);
        }

        //跳转页码
        var currentPageSize = $("span.current").html();
        $pager.append("<span class='page-skip'>到第<input id='pageJumpto' type='text' value='" + currentPageSize + "' size='3' name='jumpto'>页 ");
        var btn = $("<button type='button' id='B_pageJump' >确定</button>").click(function () {
            var currentPageIndex = isNaN($("#pageJumpto").val()) ? 1 : $("#pageJumpto").val();
            var toPageIndex = parseInt(currentPageIndex, 10);
            if (toPageIndex > pageS) {
                toPageIndex = pageS;
            }
            if (toPageIndex <= 0) {
                toPageIndex = 1;
            }
            showReviewPage(toPageIndex);
            return false;
        });

        $pager.append(btn);
        $pager.append("</span>");
    }
}


//添加到收藏夹
function addToWish(pvid, productUrl) {
    $.ajax({
        cache: false,
        type: "POST",
        dataType: "json",
        url: host + "Cart.ashx",
        data: "Target=AddToWish&ProductVariantID=" + pvid,
        error: function (data) {
            AlertWarnMsg("", "系统超时！");
        },
        success: function (data) {
            if (data.Msg == "Ok") {
                AlertSuccessMsg("", "<font style='color:green'>收藏成功！</font><a href='" + host + "wishlist.aspx' style='color:#06F;font-size:12px; font-weight:normal' target='_blank'>查看我的收藏夹>></a>");
            }
            else if (data.Msg == "BaseNeedLogin") {
                window.location = data.Obj;
            }
            else if (data.Msg == "BaseBusinessError") {
                AlertWarnMsg("", "<font>已收藏过该产品！</font><a href='" + host + "wishlist.aspx' style='color:#06F;font-size:12px; font-weight:normal' target='_blank'>查看我的收藏夹>></a>");
            }
            else
                AlertWarnMsg("", data.Obj);
        }
    });
}
//添加到对比
function addToCompareList(pvid) {
    $.ajax({
        cache: false,
        type: "POST",
        dataType: "json",
        url: host + "Handler/Product.ashx",
        data: "Target=CompareList&ProductVariantID=" + pvid,
        success: function (data) {
            if (data.Msg == "Ok") {
                window.location = host + "compareproducts.aspx";
            }
            else {
                AlertWarnMsg("", data.Msg);
            }
        }
    });
}
//给产品评级
function SetProductRating(pid, type) {
    $.ajax({
        cache: false,
        type: "post",
        dataType: "json",
        url: host + "Handler/Product.ashx",
        data: "Target=SetRating&ProductId=" + pid + "&type=" + type,
        error: function (data) { },
        success: function (data) {
            if (data.Msg == "Ok") {
                Message("评分成功！谢谢您的参与");
            }
            else {
                Message(data.Msg);
            }
        }
    });

}

$("#stars i").each(function (i, v) {
    $(v).click(function (e) {
        var stars = $("#stars");
        stars.removeClass();
        stars.addClass("stars").addClass("star" + (i + 1));
        stars.data("rang", (i + 1));
    })
})